/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.tables;

import javax.swing.table.AbstractTableModel;

import autoteam.Child;
import autoteam.League;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class SkillEditorTableModel extends AbstractTableModel
{
    
    private static final long serialVersionUID = -3224945677006469321L;
    
    Double[] vals = null;
    static String s = "Skill";
    Child theChild;
    League doc = null;
    
    public SkillEditorTableModel( Child c, League _doc )
    {
	doc = _doc;
	vals = new Double[c.skill.length];
	setChild( c, _doc );
    }
    
    public void setChild( Child c, League _doc )
    {
	theChild = c;
	doc = _doc;
	for ( int i = 0; i < vals.length; i++ )
	    vals[i] = new Double( c.skill[i].doubleValue( ) );
    }
    
    public void transfer( )
    {
	theChild.totalSkill = 0;

	
	
	for ( int i = 0; i < vals.length; i++ )
	{
	    if ( theChild.skill[i] != vals[i] ) // not both null
		if ( (theChild.skill[i] == null) || (vals[i] == null) || (!theChild.skill[i].equals( vals[i] )) )
		{
		    doc.setDirty( );
		    theChild.skill[i] = vals[i];
		}
	    theChild.totalSkill += vals[i].doubleValue( ) * theChild.getWeight( i );
	}
	
    }
    
    public int getColumnCount( )
    {
	return 2;
    }
    
    public int getRowCount( )
    {
	return vals.length;
    }
    
    public String getColumnName( int col )
    {
	return (col == 0 ? "Skill" : "Value");
    }
    
    public Object getValueAt( int row, int col )
    {
	if ( col == 0 )
	{
	    return doc.getSkillName( row );
	}
	return vals[row];
    }
    
    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
    public Class getColumnClass( int c )
    {
	return (c == 0 ? getColumnName( 0 ).getClass( ) : Double.class);
    }
    
    /*
     * Don't need to implement this method unless your table's editable.
     */
    public boolean isCellEditable( int row, int col )
    {
	return (col == 1);
    }
    
    public void setValueAt( Object value, int row, int col )
    {
	vals[row] = (Double) value;
	System.out.println( "Vals[" + row + "] = " + vals[row] );
    }
}
